<?php

/**
 * @property-read int $ID
 * @property-read string $Username
 * @property-read string $Password
 * @property-read string $RealName
 * @property-read Zend_Date $Birthday
 * @property-read string $Email
 * @property-read int $MobilePhone
 * @property-read Zend_Date $LastLogout
 * @property-read Zend_Date $CreatedDate
 * @property-read bool $Confirmed
 * @property-read int $Enabled
 * @property-read int $UserTypeID
 * @property-read bool $IsGuest
 */
class JUser
{
    /** @var int */
    protected $id;

    /** @var string */
    protected $username;

    /** @var string */
    protected $password;

    /** @var string */
    protected $realName;

    /** @var Zend_Date */
    protected $birthday;

    /** @var string */
    protected $email;

    /** @var int */
    protected $mobilePhone;

    /** @var Zend_Date */
    protected $lastLogout;

    /** @var Zend_Date */
    protected $createdDate;

    /** @var bool */
    protected $confirmed;

    /** @var int */
    protected $enabled;

    /** @var int */
    protected $userTypeID;

    /** @var JUser[] */
    protected static $instances = array();

    /**
     * @param int $id
     * @return JUser
     */
    protected function __construct($id = 0)
    {
        $this->id = 0;
        $this->username = 'guest';
        $this->password = '';
        $this->realName = 'guest';
        $this->birthday = null;
        $this->email = 'guest@guest';
        $this->mobilePhone = 0;
        $this->lastLogout = null;
        $this->createdDate = null;
        $this->confirmed = false;
        $this->enabled = 1;
        $this->userTypeID = 0;

        if ($id != 0) {
            $userPOJO = Application_Model_User::getUser($id);
            if ($userPOJO != null) {
                $this->id = $userPOJO->ID;
                $this->username = $userPOJO->Username;
                $this->password = $userPOJO->Password;
                $this->realName = $userPOJO->RealName;
                $this->birthday = $userPOJO->Birthday;
                $this->email = $userPOJO->Email;
                $this->mobilePhone = $userPOJO->MobilePhone;
                $this->lastLogout = $userPOJO->LastLogout;
                $this->createdDate = $userPOJO->CreatedDate;
                $this->confirmed = $userPOJO->Confirmed;
                $this->enabled = $userPOJO->Enabled;
                $this->userTypeID = $userPOJO->UserTypeID;
            }
        }
    }

    /**
     * @param $name
     * @return mixed
     * @throws Exception
     */
    public function __get($name)
    {
        switch ($name) {
            case 'ID':
                return $this->id;
            case 'Username':
                return $this->username;
            case 'Password':
                return $this->password;
            case 'RealName':
                return $this->realName;
            case 'Birthday':
                return $this->birthday;
            case 'Email':
                return $this->email;
            case 'MobilePhone':
                return $this->mobilePhone;
            case 'LastLogout':
                return $this->lastLogout;
            case 'CreatedDate':
                return $this->createdDate;
            case 'Confirmed':
                return $this->confirmed;
            case 'Enabled':
                return $this->enabled;
            case 'UserTypeID':
                return $this->userTypeID;
            case 'IsGuest':
                if ($this->id == 0) {
                    return true;
                } else {
                    return false;
                }
            default:
                throw new Exception("cannot get $name property");
        }
    }

    /**
     * Tham khảo Joomla 2.5
     * @param $action
     * @param $asset
     * @return bool
     */
    public function authorise($action, $asset = null)
    {
        if (isset($asset)) {
            // Mở rộng theo Joomla 2.5 sau này
        }

        $acl = JACL::factory();

        return $acl->isAllowed((string)$this->UserTypeID, $action);
    }

    /**
     * @return int
     */
    public function getNumberOfProcessedOrders()
    {
        if ($this->IsGuest) {
            return 0;
        }

        return Application_Model_Order::getNumberOfProcessedOrders(
            $this->lastLogout, $this->id
        );
    }

    /**
     * @param string $content
     * @param int $productID
     * @return int
     */
    public function addComment($content, $productID)
    {
        if ($this->IsGuest) {
            return 0;
        }

        return Application_Model_Comment::addComment(
            $content, $this->username, $productID, $this->id
        );
    }

    /**
     * @return bool
     */
    public function confirm()
    {
        if ($this->IsGuest) {
            return false;
        }

        if (!Application_Model_User::confirmUser($this->id)) {
            return false;
        }

        $this->confirmed = true;

        return true;
    }

    /**
     * @return bool
     */
    public function updateLastLogout()
    {
        if ($this->IsGuest) {
            return false;
        }

        return Application_Model_User::updateLastLogout(
            $this->id, new Zend_Date()
        );
    }

    /**
     * @param int $id
     * @return JUser
     */
    public static function getInstance($id = 0)
    {
        if (!isset(self::$instances[$id])) {
            self::$instances[$id] = new JUser($id);
        }

        return self::$instances[$id];
    }
}
